<?php
/**
 * Created by PhpStorm.
 * User: aha
 * Date: 2017/8/15
 * Time: 19:27
 */

namespace Exam\Object;


use Exam\Model\QuestionModel;
use Tsy\Library\Object;
use Exam\Model\PlanModel;

class RealexamObject extends Object
{

    protected $main='Realexam';
    protected $pk='RID';
    public $allow_add=true;
    public $addFields=['UID','SID','SIDs'];//允许添加的字段，如果数组最后一个元素值为true则表示排除
    public $saveFields=['UID','SID','SIDs'];//允许修改的字段，如果数组最后一个元素值为true则表示排除
   
  
    protected $property=[
        'User'=>[//创建人ID  用户  属性
            self::RELATION_TABLE_NAME=>'User',//属性关联表
            self::RELATION_TABLE_COLUMN=>'UID',//关联表中的关联字段
            self::RELATION_MAIN_COLUMN=>'UID',//主笔中的关联字段
            self::RELATION_TABLE_PROPERTY=>self::PROPERTY_ONE_OBJECT,
        ],
        'Exampaper'=>[//创建人ID  用户  属性
            self::RELATION_TABLE_NAME=>'Exampaper',//属性关联表
            self::RELATION_TABLE_COLUMN=>'SID',//关联表中的关联字段
            self::RELATION_MAIN_COLUMN=>'SID',//主笔中的关联字段
            self::RELATION_TABLE_PROPERTY=>self::PROPERTY_ONE_OBJECT,
        ],
    ];
      protected $searchFields=['UID','SID'];
      protected $searchTable='User';
      protected $searchWFieldsConf=[
        'User'=>'User',
      ];
      protected $searchWFieldsGroup=[
       'User'=>['Name'],
      ];
   /* function search($data = [], $Properties = false, $OnlyPK = false)
    {
        $data = array_merge($data,$_POST);
        if ($data['RID'] != null) {
            $condition = [
                'RID' => $data['RID']
            ];
        }
        if ($data['SIDs'] != null) {
            $condition = [
                'SIDs' => $data['SIDs']
            ];
        }
        if ($data['UID'] != null) {
            $condition = [
                'UID' => $data['UID']
            ];
        }
        if ($data['SID'] != null) {
            $condition = [
                'SID' => $data['SID']
            ];
        }
        $realexam = M('realexam');
        $score = $realexam->where($condition)->select();
        if ($score != null) {
            return $score;
        } else {
            return "没有查到相关试卷!";
        }
    }*/
    function generateSJID($SID){
        $temp = new ExampaperObject();
        $sj = $temp->get($SID);
        $Plan = $sj['Plan'];
        if (!is_array($Plan)){
            return "未获得该方案";
        }
        $result = [
            'DX'=>[],
            'DDX'=>[],
            'PD'=>[],
            'DT'=>[],
            'ZGT'=>[],
        ];
        $planModel = new PlanModel();
//        $question = new QuestionObject();
        //单选题
        if ($Plan['DXnumber']>0){
            $XZ =  $Plan['DXnumber'];
            $result['DX'] =  $planModel->query("SELECT SID FROM question WHERE TTID=2 ORDER BY RAND() LIMIT $XZ");
            if (count($result['DX'])<$XZ){
                $name = '单选题';
                return "题库里面$name 的数量未能达到方案的要求数量。";
            }
            for ($i=0; $i<count($result['DX']); $i++){
                $result['DX'][$i] = $result['DX'][$i]['SID'];
            }
        }
        //多选题
        if ($Plan['DDXnumber']>0){
            $XZ =  $Plan['DDXnumber'];
            $result['DDX'] =  $planModel->query("SELECT SID FROM question WHERE TTID=3 ORDER BY RAND() LIMIT $XZ");
            if (count($result['DDX'])<$XZ){
                $name = '多选题';
                return "题库里面$name 的数量未能达到方案的要求数量。";
            }
            for ($i=0; $i<count($result['DDX']); $i++){
                $result['DDX'][$i] = $result['DDX'][$i]['SID'];
            }
        }
        //判断题
        if ($Plan['PDnumber']>0){
            $XZ =  $Plan['PDnumber'];
            $result['PD'] =  $planModel->query("SELECT SID FROM question WHERE TTID=6 ORDER BY RAND() LIMIT $XZ");
            if (count($result['PD'])<$XZ){
                $name = '判断题';
                return "题库里面$name 的数量未能达到方案的要求数量。";
            }
            for ($i=0; $i<count($result['PD']); $i++){
                $result['PD'][$i] = $result['PD'][$i]['SID'];
            }

        }

        //主观题
        if ($Plan['ZGnumber']>0){
            $XZ =  $Plan['ZGnumber'];
            $result['ZGT'] =  $planModel->query("SELECT SID FROM question WHERE TTID=7 ORDER BY RAND() LIMIT $XZ");
            if (count($result['ZGT'])<$XZ){
                $name = '主观题';
                return "题库里面$name 的数量未能达到方案的要求数量。";
            }
            for ($i=0; $i<count($result['ZGT']); $i++){
                $result['ZGT'][$i] = $result['ZGT'][$i]['SID'];
            }

        }

        //大题
        if ($Plan['DTnumber']>0){
            $XZ =  $Plan['DTnumber'];
            $result['DT'] =  $planModel->query("SELECT SID FROM question WHERE TTID=4 ORDER BY RAND() LIMIT $XZ");
            if (count($result['DT'])<$XZ){
                $name = '大题';
                return "题库里面$name 的数量未能达到方案的要求数量。";
            }
            for ($i=0; $i<count($result['DT']); $i++){
                $result['DT'][$i] = $result['DT'][$i]['SID'];
            }
        }
        return $result;
    }

    /**
     * 生成试卷所有的题号。
     * @param $SID 试卷ID
     * @return array|string
     */
    function generateNozsd($SID){
        $temp = new ExampaperObject();
        $sj = $temp->get($SID);
        $Plan = $sj['Plan'];
        $result = [
            'DX'=>[],
            'DDX'=>[],
            'PD'=>[],
            'DT'=>[],
            'ZGT'=>[],
        ];
        $planModel = new PlanModel();
//       $result['单选题'] =
        $question = new QuestionObject();
        //单选题
        if ($Plan['DXnumber']>0){
            $XZ =  $Plan['DXnumber'];
            $result['DX'] =  $planModel->query("SELECT SID FROM question WHERE TTID=2 ORDER BY RAND() LIMIT $XZ");
            if (count($result['DX'])<$XZ){
                $name = '单选题';
                return "题库里面$name 的数量未能达到方案的要求数量。";
            }
            for ($i=0; $i<count($result['DX']); $i++){
                $result['DX'][$i] = $question->get($result['DX'][$i]['SID']);
            }
        }
        //多选题
        if ($Plan['DDXnumber']>0){
            $XZ =  $Plan['DDXnumber'];
            $result['DDX'] =  $planModel->query("SELECT * FROM question WHERE TTID=3 ORDER BY RAND() LIMIT $XZ");

            if (count($result['DDX'])<$XZ){
                $name = '多选题';
                return "题库里面$name 的数量未能达到方案的要求数量。";
            }
            for ($i=0; $i<count($result['DDX']); $i++){
                $result['DDX'][$i] = $question->get($result['DDX'][$i]['SID']);
            }
        }
        //判断题
        if ($Plan['PDnumber']>0){
            $XZ =  $Plan['PDnumber'];
            $result['PD'] =  $planModel->query("SELECT * FROM question WHERE TTID=6 ORDER BY RAND() LIMIT $XZ");
            if (count($result['PD'])<$XZ){
                $name = '判断题';
                return "题库里面$name 的数量未能达到方案的要求数量。";
            }

        }

        //主观题
        if ($Plan['ZGnumber']>0){
            $XZ =  $Plan['ZGnumber'];
            $result['PD'] =  $planModel->query("SELECT * FROM question WHERE TTID=7 ORDER BY RAND() LIMIT $XZ");
            if (count($result['PD'])<$XZ){
                $name = '主观题';
                return "题库里面$name 的数量未能达到方案的要求数量。";
            }

        }

        //大题
        if ($Plan['DTnumber']>0){
            $XZ =  $Plan['DTnumber'];
            $result['DT'] =  $planModel->query("SELECT * FROM question WHERE TTID=4 ORDER BY RAND() LIMIT $XZ");
            if (count($result['DT'])<$XZ){
                $name = '大题';
                return "题库里面$name 的数量未能达到方案的要求数量。";
            }
            for ($i=0; $i<count($result['DT']); $i++){
                $result['DT'][$i] = $question->get($result['DT'][$i]['SID']);
//                for ($j=0; $j<count($result['DT'][$i]['Question']); $j++){
//                    $result['DT'][$i]['Question'][$j] = $question->get($result['DT'][$i]['Question'][$j]['SID']);
//                }
            }
        }
        return $result;
    }

    //随机生成
    function creatSJ($SID){
        $km = M('exampaper')->where("SID=$SID")->field('KID')->find();
        switch ($km['KID']){
            case 2:
                return $this->generateNozsd($SID);
            case 3:

                break;
            case 4:
                return $this->generateNozsd($SID);
            case 5:

                break;
        }
        return false;
    }

    /** 随机生成两套试卷。
     * @param int $SID
     * @param int $count
     * @return mixed
     */
    function creatEXdb($UID,$SID=0,$count=1){
        startTrans();
        $exdb=[];
        for($i=0; $i<$count; $i++){
            $ex = $this->generateSJID($SID);
            $re = "dx:";
            foreach ($ex['DX'] as $DX){
                $re.=(",".$DX);
            }
            $re .= ";ddx:";
            foreach ($ex['DDX'] as $DX){
                $re.=(",".$DX);
            }
            $re .= ";pd:";
            foreach ($ex['ZGT'] as $DX){
                $re.=(",".$DX);
            }
            $re .= ";dt:";
            foreach ($ex['DT'] as $DX){
                $re.=(",".$DX);
            }
            $re .= ";zgt:";
            foreach ($ex['ZGT'] as $DX){
                $re.=(",".$DX);
            }
            $re .= ";";
            $data =[
                'UID'=>$UID,
                'SID'=>$SID,
                'SIDs'=>$re
            ];
//            $SIDs = $re;
            $result = M('realexam')->data($data)->add();
            if (!is_numeric($result)){
                rollback();
                return "生成题库失败";
            }
            $exdb[] = $result;
        }
        commit();
        return $exdb;
    }

    function get($ID = false)
    {
//        if ()
        $re = M('realexam')->where("RID=$ID")->find();
        if (!is_array($re)){
            return "未找到该ID对应的数据.";
        }
        $SIDs = $re['SIDs'];
        $SIDs = explode(';',$SIDs);
        $db = [];
        $name =[            'DX','DDX', 'PD', 'DT', 'ZGT',        ];
        $Question = new QuestionObject();
        for($i=0; $i<count($SIDs); $i++){
            $db[$i] = explode(':',$SIDs[$i]);
            $db[$i] = explode(",",$db[$i][1]);
            for($j=0; $j<count($db[$i]); $j++){
                $db[$i][$j] = $Question->get($db[$i][$j]);
            }
        }
        $re['DX'] = $db[0];
        $re['DDX']= $db[1];
        $re['PD']= $db[2];
        $re['DT']= $db[3];
        $re['ZGT']= $db[4];
        return $re;
    }

    //根据知识点划分题目
    function generateZSD($SID){
        $temp = new ExampaperObject();
        $sj = $temp->get($SID);
        $Plan = $sj['Plan'];
        if (!is_array($Plan)){
            return "未获得该方案";
        }
        $result = [
            'DX'=>[],
            'DDX'=>[],
            'PD'=>[],
            'DT'=>[],
            'ZGT'=>[],
        ];
//        $questionCounts =new Ques;
        $questionModel = new QuestionModel();
        $KID = $sj['KID'];
        $QuestionCount['DXnumber'] =  $questionModel->query("SELECT count(SID) as dxCount FROM question WHERE KID=$KID AND TTID=2");
        $QuestionCount['DXnumber'] =  $QuestionCount['DXnumber'][0]['dxCount'];
        $QuestionCount['DDXnumber'] =  $questionModel->query("SELECT count(SID) as dxCount FROM question WHERE KID=$KID AND TTID=3");
        $QuestionCount['DDXnumber'] =  $QuestionCount['DDXnumber'][0]['dxCount'];
        $QuestionCount['DTnumber'] =  $questionModel->query("SELECT count(SID) as dxCount FROM question WHERE KID=$KID AND TTID=4");
        $QuestionCount['DTnumber'] =  $QuestionCount['DTnumber'][0]['dxCount'];
        $QuestionCount['PDnumber'] =  $questionModel->query("SELECT count(SID) as dxCount FROM question WHERE KID=$KID AND TTID=6");
        $QuestionCount['PDnumber'] =  $QuestionCount['PDnumber'][0]['dxCount'];
        $QuestionCount['ZGnumber'] =  $questionModel->query("SELECT count(SID) as dxCount FROM question WHERE KID=$KID AND TTID=7");
        $QuestionCount['ZGnumber'] =  $QuestionCount['ZGnumber'][0]['dxCount'];
        foreach ($QuestionCount as $key=>$value){
            if ($QuestionCount[$key] < $Plan[$key]){
                return "在数据库中的题小于方案的题，无法出题";
            }
        }
        $zsd = $questionModel->query("SELECT ZSDID FROM question WHERE KID=$KID");
        
        return $result;
    }

}